<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="生产月份" prop="scrq">
        <el-date-picker
          :editable="false"
          clearable
          size="small"
          v-model="queryParams.scrq"
          type="monthrange"
          range-separator="至"
          start-placeholder="开始月份"
          end-placeholder="结束月份"
          value-format="yyyy-MM"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="单班内控指标" prop="signalnk">
        <el-input
          v-model="queryParams.signalnk"
          placeholder="请输入单班内控指标"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单班产出平均值" prop="signalavg">
        <el-input
          v-model="queryParams.signalavg"
          placeholder="请输入单班产出平均值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="全员内控指标" prop="sumnk">
        <el-input
          v-model="queryParams.sumnk"
          placeholder="请输入全员内控指标"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="全员产出平均值" prop="sumavg">
        <el-input
          v-model="queryParams.sumavg"
          placeholder="请输入全员产出平均值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="达成率" prop="dcl">
        <el-input
          v-model="queryParams.dcl"
          placeholder="请输入达成率"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原因分析" prop="reasons">
        <el-input
          v-model="queryParams.reasons"
          placeholder="请输入原因分析"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="改进措施" prop="meansures">
        <el-input
          v-model="queryParams.meansures"
          placeholder="请输入改进措施"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属基地" prop="jd">
        <el-input
          v-model="queryParams.jd"
          placeholder="请输入所属基地"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['analysis_singelsum:analysis_singelsum:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['analysis_singelsum:analysis_singelsum:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['analysis_singelsum:analysis_singelsum:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['analysis_singelsum:analysis_singelsum:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="analysis_singelsumList"
      @selection-change="handleSelectionChange"
      max-height="500"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" width="100px" />
      <el-table-column label="生产月份" align="center" prop="scrq" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scrq, "{y}-{m}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单班内控指标" align="center" prop="signalnk" />
      <el-table-column label="单班产出平均值" align="center" prop="signalavg" />
      <!-- <el-table-column label="全员内控指标" align="center" prop="sumnk" />
      <el-table-column label="全员产出平均值" align="center" prop="sumavg" />
      <el-table-column label="达成率" align="center" prop="dcl" />
      <el-table-column label="原因分析" align="center" prop="reasons" />
      <el-table-column label="改进措施" align="center" prop="meansures" />
      <el-table-column label="所属基地" align="center" prop="jd" /> -->
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="300px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="excelhandleView(scope.row)"
            >导出</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['analysis_singelsum:analysis_singelsum:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['analysis_singelsum:analysis_singelsum:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仰口生产报汇总对话框 -->
    <el-dialog
      fullscreen
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
      custom-class="myDialog"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产日期" prop="scrq">
              <el-date-picker
                :editable="false"
                clearable
                v-model="form.scrq"
                type="month"
                value-format="yyyy-MM"
                placeholder="选择月份"
                :disabled="openDisabled"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="单班内控指标" prop="signalnk">
              <el-input
                type="number"
                v-model="form.signalnk"
                placeholder="请输入单班内控指标"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单班产出平均值" prop="signalavg">
              <el-input
                type="number"
                v-model="form.signalavg"
                placeholder="请输入单班产出平均值"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="全员内控指标" prop="sumnk">
              <el-input
                type="number"
                v-model="form.sumnk"
                @change="subtraction(form)"
                placeholder="请输入全员内控指标"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="全员产出平均值" prop="sumavg">
              <el-input
                type="number"
                v-model="form.sumavg"
                @change="subtraction(form)"
                placeholder="请输入全员产出平均值"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="达成率" prop="dcl">
              <el-input
                type="number"
                v-model="form.dcl"
                placeholder="请输入达成率"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="原因分析" prop="reasons">
              <el-input
                v-model="form.reasons"
                placeholder="请输入原因分析"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="改进措施" prop="meansures">
              <el-input
                v-model="form.meansures"
                placeholder="请输入改进措施"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属基地" prop="jd">
              <!-- <el-input
                v-model="form.jd"
                placeholder="请输入所属基地"
                :disabled="openDisabled"
              /> -->
              <el-select
                v-model="form.jd"
                placeholder="请选择所属工厂"
                clearable
                size="small"
                :disabled="true"
              >
                <el-option
                  v-for="dict in factoryListOptions"
                  :key="dict.deptId"
                  :label="dict.name"
                  :value="dict.deptId"
                ></el-option>
              </el-select>
              <!--<el-select v-model="form.jd" placeholder="请选择所属基地" clearable size="small" :disabled="openDisabled">
                <el-option v-for="dict in factoryListOptions" :key="dict.deptId" :label="dict.name" :value="dict.deptId"></el-option>
              </el-select>-->
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAnalysis_singelsum,
  getAnalysis_singelsum,
  delAnalysis_singelsum,
  addAnalysis_singelsum,
  updateAnalysis_singelsum,
  getFactoryList,
} from "@/api/report/analysis/analysis_singelsum";

export default {
  name: "Analysis_singelsum",
  data() {
    return {
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 所属工厂
      factoryListOptions: [],
      // 总条数
      total: 0,
      // 仰口生产报汇总表格数据
      analysis_singelsumList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        scrq: null,
        scrq1: null,
        signalnk: null,
        signalavg: null,
        sumnk: null,
        sumavg: null,
        dcl: null,
        reasons: null,
        meansures: null,
        jd: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.getFactoryList();
  },

  methods: {
    //工厂信息
    getFactoryList() {
      getFactoryList().then((response) => {
        this.factoryListOptions = response.data;
      });
    },
    subtraction(form) {
      if (Number(form.sumnk) == 0) {
        form.dcl = 0;
      } else {
        form.dcl =
          (form.sumavg == null ? 0 : Number(form.sumavg)) /
          (form.sumnk == null ? 0 : Number(form.sumnk));
      }
    },
    /** 查询仰口生产报汇总列表 */
    getList() {
      this.loading = true;
      listAnalysis_singelsum(this.queryParams).then((response) => {
        this.analysis_singelsumList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        scrq: null,
        signalnk: null,
        signalavg: null,
        sumnk: null,
        sumavg: null,
        dcl: null,
        reasons: null,
        meansures: null,
        jd: null,
        delFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.scrq != null) {
        let arr = this.queryParams.scrq;
        this.queryParams.scrq = arr[0];
        this.queryParams.scrq1 = arr[1];
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加仰口生产报汇总";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getAnalysis_singelsum(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仰口生产报汇总";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getAnalysis_singelsum(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "查看仰口生产报汇总";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAnalysis_singelsum(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAnalysis_singelsum(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除仰口生产报汇总编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delAnalysis_singelsum(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    excelhandleView(row) {
      this.download(
        "productionProcess/analysis_singelsum/exportexcel?id=" + row.id,
        {
          ...this.queryParams,
        },
        "生产产出统计月报表汇总" + this.parseTime(new Date()) + ".xlsx"
      );
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "analysis_singelsum/analysis_singelsum/export",
        {
          ...this.queryParams,
        },
        "仰口生产报汇总" + this.parseTime(new Date()) + ".xlsx"
      );
    },
  },
};
</script>
